package com.small.sentinel;

import com.small.common.designPattern.behavioral.chainOfResponsibility.slotchain.ProcessorSlot;
import com.small.sentinel.context.NullContext;
import com.small.sentinel.node.Node;
import com.small.sentinel.slotchain.ResourceWrapper;

public class CtEntry extends Entry {

    /**
     * 父节点
     */
    protected Entry parent = null;

    /**
     * 子节点
     */
    protected Entry child = null;

    /**
     * 执行链
     */
    protected ProcessorSlot<Object> chain;

    /**
     * 上下文
     */
    protected Context context;

    CtEntry(ResourceWrapper resourceWrapper, ProcessorSlot<Object> chain, Context context) {
        super(resourceWrapper);
        this.chain = chain;
        this.context = context;
        this.setUpEntryFor(context);
    }

    private void setUpEntryFor(Context context) {

        if (context instanceof NullContext) {
            return;
        }
        this.parent = context.getCurEntry();
        if (parent != null) {
            ((CtEntry) parent).child = this;
        }
        context.setCurEntry(this);
    }


    @Override
    public Node getLastNode() {
        return parent == null ? null : parent.getCurNode();
    }
}
